package com.yunhe.abnormal.controller.base;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.base.AlarmTypeDTO;
import com.yunhe.abnormal.service.base.AlarmTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 异常类型Controller
 * @author liuronglei
 */
@Api(value = "alarmType", description = "异常类型", tags = {"Base_AlarmType"})
@RestController
@RequestMapping("/alarmTypes")
public class AlarmTypeController {
    @Autowired
    private AlarmTypeService alarmTypeService;

    /**
     * 新增异常类型对象
     * @param alarmType 异常类型对象
     */
    @ApiOperation("新增异常类型对象")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> create(@RequestBody AlarmTypeDTO alarmType)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmTypeDTO newAlarmType = alarmTypeService.create(alarmType);
        return new ResponseEntity<>(new ResultObject<>(newAlarmType), HttpStatus.OK);
    }

    /**
     * 根据异常类型ID删除异常类型
     * @param id 异常类型ID
     */
    @ApiOperation("根据异常类型ID删除异常类型")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        alarmTypeService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据异常类型ID批量获得异常类型
     * @param ids 异常类型ID
     */
    @ApiOperation("根据异常类型ID批量获得异常类型")
    @ApiImplicitParam(name = "ids", value = "异常类型ID", required = true, dataType = "long", paramType = "query")
    @RequestMapping(value = "/batch", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> batchDelete(@RequestParam("ids") List<Long> ids) {
        alarmTypeService.batchDelete(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新异常类型
     * @param id 异常类型ID
     * @param title 异常类型标题
     * @param desc 异常类型描述
     */
    @ApiOperation("更新异常类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常类型ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "title", value = "异常类型标题", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "desc", value = "异常类型描述", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam("title") String title,
                                              @RequestParam("desc") Optional<String> desc)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmTypeDTO newAlarmType = alarmTypeService.update(id, title, desc.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(newAlarmType), HttpStatus.OK);
    }

    /**
     * 更新异常类型
     * @param id 异常类型ID
     * @param alarmType 异常类型
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ApiOperation("更新异常类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常类型ID", required = true, dataType = "long", paramType = "path")
    })
    public ResponseEntity update(@PathVariable("id") Long id,
                                 @RequestBody AlarmTypeDTO alarmType)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        alarmType.setId(id);
        AlarmTypeDTO newAlarmType = alarmTypeService.update(alarmType);
        return new ResponseEntity<>(new ResultObject<>(newAlarmType), HttpStatus.OK);
    }

    /**
     * 根据异常类型ID获得异常类型
     * @param id 异常类型ID
     */
    @ApiOperation("根据异常类型ID获得异常类型")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AlarmTypeDTO alarmType = alarmTypeService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(alarmType), HttpStatus.OK);
    }

    /**
     * 根据条件获得异常类型列表
     * @param queryStr 查询关键字
     * @param dtime 维护时间
     * @param pageParam 分页对象
     */
    @ApiOperation("根据条件获得异常类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryStr", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "维护时间", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("queryStr") Optional<String> queryStr,
                                               @RequestParam("dtime") Optional<List<String>> dtime,
                                               @ModelAttribute PageParam pageParam) {
        Iterable<AlarmTypeDTO> results = alarmTypeService.search(queryStr.orElse(null), dtime.orElse(null), pageParam);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 已绑定的异常类型ID列表
     * @param ids 异常类型ID
     */
    @ApiOperation("已绑定的异常类型ID列表")
    @RequestMapping(value = "/boundRule", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> boundRule(@RequestParam("ids") List<Long> ids) {
        List<Long> results = alarmTypeService.getBoundRule(ids);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }
}
